/**
 * Created by limeng on 16/4/26.
 */
delegateApp.controller('ViewMessagesCtrl', function ($scope, $rootScope, $http) {
    $scope.pageSize = 10;
    $scope.pageNo = 1;
    $scope.inputPageNo = 1;
    $scope.pageCount = 1;
    $scope.messages = [];

    $scope.updateContent = function () {
        $http.get('/messages/pagination/' +
            $scope.pageSize + '/' +
            $scope.pageNo
        ).success(
            function (data) {
                $scope.messages = data.messages;
                $scope.pageCount = data.pageCount;
            }
        ).error(
            function () {
                showAlert('查询错误', '请联系开发者维护!');
            }
        );
    };

    $scope.showDialog = function(message) {
        if (message.unread == true) {
            $http.get('/messages/setRead/' + message.id).success(function (data) {
                if (data.state == true) {
                    message.unread = false;
                }
            });
        }
        $scope.readingMessage = message;
        $("#readMessageDialog").modal();
    };

    $scope.previousPage = function () {
        $scope.pageNo = ($scope.pageNo - 1) < 1 ? 1 : --$scope.pageNo;
        $scope.updateContent();
    };
    $scope.nextPage = function() {
        $scope.pageNo = ($scope.pageNo + 1) >= $scope.pageCount ? $scope.pageCount : ++$scope.pageNo;
        $scope.updateContent();
    };
    $scope.jumpPage = function () {
        if ($scope.inputPageNo > 0 && $scope.inputPageNo <= $scope.pageCount) {
            $scope.pageNo = parseInt($scope.inputPageNo);
            $scope.updateContent();
        } else {
            $scope.inputPageNo = 1;
        }
    };

    $scope.updateContent();
});